// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/exprs/when_clause.h>
#include <cantor/transform/exprs/when_clause_case.h>
#include <cantor/transform/expr.h>

namespace cantor::transform {

    void TransWhenClause::resolve_ast(const cantor::parser::WhenClause* node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        if (node->when_clauses.size() < 1) {
            make_missing_required_field("when_clauses");
            return;
        }
        if (node->case_expr) {
            status = TransExpr::resolve_to(node->case_expr, when_clause.mutable_case_expr());
            if (!status.ok()) {
                return;
            }
        }
        when_clause.mutable_when_clauses()->Reserve(node->when_clauses.size());
        for (size_t i = 0; i < node->when_clauses.size(); ++i) {
            TransWhenClauseCase cexpr;
            cexpr.resolve_ast(node->when_clauses[i]);
            status = cexpr.check();
            if (!status.ok()) {
                return;
            }
            auto ptr = when_clause.mutable_when_clauses()->Add();
            *ptr = std::move(cexpr.when_clause_case);
        }
        if (node->else_expr) {
            status = TransExpr::resolve_to(node->else_expr, when_clause.mutable_else_expr());
            if (!status.ok()) {
                return;
            }
        }
        set_proto_location(node, when_clause.mutable_location());
        set_proto_status(when_clause.mutable_status());
    }

    const turbo::Status &TransWhenClause::check() {
        if (!status.ok()) {
            return status;
        }
        if (!when_clause.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        if (when_clause.when_clauses().empty()) {
            make_missing_required_field("when_clauses");
        }
        return status;
    }

    turbo::Status TransWhenClause::resolve_to(turbo::Nonnull<const cantor::parser::WhenClause*> node, turbo::Nonnull<pb::ast::WhenClause*>out) {
        TransWhenClause translator;
        translator.resolve_ast(node);
        auto status = translator.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(translator.when_clause);
        return status;
    }
}  // cantor::transform
